package com.studentmanager.controller;

import com.studentmanager.domain.learningunit;
import com.studentmanager.service.learnunitservice;
import com.studentmanager.util.AjaxResult;
import com.studentmanager.util.Data;
import com.studentmanager.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/learningunit")
public class learningunitcontroller {
    @Qualifier("learnunitservice")
    @Autowired
    private learnunitservice learningunitService;

    @GetMapping("/learningunit_list")
    public String learningunitList(){
        return "learningunit/learningunit";
    }


    @PostMapping("/getUnitList")
    @ResponseBody
    public Object getUnitList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                              @RequestParam(value = "rows", defaultValue = "10") Integer rows,
                              String unitTitle,
                              @RequestParam(value = "courseId", required = false) Integer courseId,
                              @RequestParam(value = "status", required = false) Integer status) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("pageno", page);
        paramMap.put("pagesize", rows);
        if (unitTitle != null && !unitTitle.isEmpty()) paramMap.put("unitTitle", unitTitle);
        if (courseId != null) paramMap.put("courseId", courseId);
        if (status != null) paramMap.put("status", status);

        PageBean<learningunit> pageBean = learningunitService.queryPage(paramMap);

        Map<String, Object> result = new HashMap<>();
        result.put("total", pageBean.getTotalsize());
        result.put("rows", pageBean.getDatas());
        return result;
    }

    /**
     * Adds a new course learn unit.
     * @param courseLearnUnit The unit to be added.
     * @return Result of the operation.
     */
    @PostMapping("/addUnit")
    @ResponseBody
    public AjaxResult addUnit(learningunit courseLearnUnit) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            int count = learningunitService.addUnit(courseLearnUnit);
            if (count > 0) {
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("添加成功");
            } else {
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("添加失败");
        }
        return ajaxResult;
    }


    @PostMapping("/editUnit")
    @ResponseBody
    public AjaxResult editUnit(learningunit courseLearnUnit) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            int count = learningunitService.editUnit(courseLearnUnit);
            if (count > 0) {
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("修改成功");
            } else {
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("修改失败");
        }
        return ajaxResult;
    }


    @PostMapping("/deleteUnit")
    @ResponseBody
    public AjaxResult deleteUnit(Data data) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            int count = learningunitService.deleteUnits(data.getIds());
            if (count > 0) {
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("删除成功");
            } else {
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("删除失败");
        }
        return ajaxResult;
    }
}
